package app.qrcode.browser.chat.model.dao

import android.annotation.SuppressLint
import android.content.ContentValues
import android.content.Context
import app.qrcode.browser.chat.model.bean.UserInfo
import app.qrcode.browser.chat.model.db.DBHelper

/**
 * Created by DomenCai on 2022/3/12.
 */
class ContactTableDao(context: Context) {
    val mHelper = DBHelper(context, "DBHelper.db", 1)

    @SuppressLint("Range")
    fun getContacts(): List<UserInfo> {
        val db = mHelper.readableDatabase
        val sql = "select * from " +
                "${ContactTable.TAB_NAME} where " +
                "${ContactTable.COL_IS_CONTACT} =1"
        val cursor = db.rawQuery(sql, null)
        var userInfo: UserInfo?
        val users = ArrayList<UserInfo>()
        while (cursor.moveToNext()) {
            userInfo = UserInfo(
                cursor.getString(cursor.getColumnIndex(ContactTable.COL_NAME)),
                cursor.getString(cursor.getColumnIndex(ContactTable.COL_HXID)),
                cursor.getString(cursor.getColumnIndex(ContactTable.COL_NICK)),
                cursor.getString(cursor.getColumnIndex(ContactTable.COL_PHOTO))
            )
            users.add(userInfo)
        }
        cursor.close()
        return users
    }

    @SuppressLint("Range")
    fun getContactByHx(hxId: String?): UserInfo? {
        if (hxId == null) return null
        val db = mHelper.readableDatabase
        val sql: String = "select * from " +
                "${ContactTable.TAB_NAME} where " +
                "${ContactTable.COL_HXID} =?"
        val cursor = db.rawQuery(sql, listOf(hxId).toTypedArray())
        var userInfo: UserInfo? = null
        if (cursor.moveToNext()) {
            userInfo = UserInfo(
                cursor.getString(cursor.getColumnIndex(UserAccountTable.COL_NAME)),
                cursor.getString(cursor.getColumnIndex(UserAccountTable.COL_HXID)),
                cursor.getString(cursor.getColumnIndex(UserAccountTable.COL_NICK)),
                cursor.getString(cursor.getColumnIndex(UserAccountTable.COL_PHOTO))
            )
        }
        cursor.close()
        return userInfo
    }

    fun getContactsByHx(hxIds: List<String>?): List<UserInfo>? {
        if (hxIds == null || hxIds.isEmpty()) {
            return null
        }
        val contacts = ArrayList<UserInfo>()

        hxIds.forEach { hxid: String ->
            val contact = getContactByHx(hxid)
            if (contact != null) {
                contacts.add(contact)
            }
        }
        return contacts
    }

    fun saveContact(user: UserInfo?, isMyContact: Boolean) {
        if (user == null) {
            return
        }

        val db = mHelper.readableDatabase
        val values = ContentValues().apply {
            put(ContactTable.COL_HXID, user.hxId)
            put(ContactTable.COL_NAME, user.name)
            put(ContactTable.COL_NICK, user.nick)
            put(ContactTable.COL_PHOTO, user.photo)
            put(ContactTable.COL_IS_CONTACT, if (isMyContact) 1 else 0)
        }
        db.replace(ContactTable.TAB_NAME, null, values)
    }

    fun saveContacts(contacts: List<UserInfo>?, isMyContact: Boolean) {
        if (contacts == null || contacts.isEmpty()) {
            return
        }
        contacts.forEach {
            saveContact(it, isMyContact)
        }

    }

    fun deleteContactByHxId(hxId: String?) {
        if (hxId == null) {
            return
        }
        val db = mHelper.readableDatabase
        db.delete(ContactTable.TAB_NAME, "${ContactTable.COL_HXID} = ", listOf(hxId).toTypedArray())
    }
}